﻿using System;
using System.Windows.Forms;

namespace Связь_с_БД
{
    public partial class FormAddRow : Form
    {
        public FormAddRow()
        {
            InitializeComponent();
        }
        private string[] _documentStudentValue;
        private string[] _examsValue;
        private string[] _jobsValue;
        private string[] _learningDataValue;
        private string[] _learningValue;
        private string[] _learningDataDocumentValue;
        private string[] _parentsValue;
        private string[] _paymentsValue;
        private string[] _previousEducationValue;
        private string[] _privateDataValue;
        private const string DateEmptyShort = @"  .  .";
        private const string DateEmptyLong = @"  .  .       :";
        private const string DateShort = @"1950-01-01";
        private const string DateLong = @"1950-01-01 0:00:00";

        private void Button2Click(object sender, EventArgs e)
        {
            Close();
        }

        private void SetDocumentStudentValue()
        {
            _documentStudentValue = new string[10];
            _documentStudentValue[0] = textBox75.Text;
            _documentStudentValue[1] = textBox74.Text;
            _documentStudentValue[2] = textBox73.Text;
            _documentStudentValue[3] = textBox72.Text;
            _documentStudentValue[4] = textBox70.Text;
            _documentStudentValue[5] = textBox71.Text;
            _documentStudentValue[6] = textBox69.Text;
            _documentStudentValue[7] = textBox68.Text;
            _documentStudentValue[8] = textBox67.Text;
            _documentStudentValue[9] = textBox65.Text;
        }
        private void SetExamsValue()
        {
            _examsValue = new string[9];
            _examsValue[0] = textBox89.Text;
            _examsValue[1] = textBox87.Text;
            _examsValue[2] = textBox88.Text;
            _examsValue[3] = textBox86.Text;
            _examsValue[4] = textBox85.Text;
            _examsValue[5] = textBox84.Text;
            _examsValue[6] = textBox82.Text;
            _examsValue[7] = textBox80.Text;
            _examsValue[8] = textBox81.Text;
        }
        private void SetJobsValue()
        {
            _jobsValue = new string[6];
            _jobsValue[0] = textBox12.Text;
            _jobsValue[1] = textBox66.Text;
            _jobsValue[2] = textBox79.Text;
            _jobsValue[3] = textBox78.Text;
            _jobsValue[4] = textBox77.Text;
            _jobsValue[5] = textBox76.Text;
        }
        private void SetLearningDataValue()
        {
            _learningDataValue = new string[13];
            _learningDataValue[0] = textBox44.Text;
            _learningDataValue[1] = textBox43.Text;
            _learningDataValue[2] = textBox42.Text;
            _learningDataValue[3] = textBox41.Text;
            _learningDataValue[4] = textBox40.Text;
            _learningDataValue[5] = textBox39.Text;
            _learningDataValue[6] = textBox35.Text;
            _learningDataValue[7] = textBox25.Text;
            _learningDataValue[8] = textBox24.Text;
            _learningDataValue[9] = textBox23.Text;
            _learningDataValue[10] = textBox22.Text;
            _learningDataValue[11] = textBox21.Text;
            _learningDataValue[12] = string.Empty;
        }
        private void SetLearningValue()
        {
            _learningValue = new string[9];
            _learningValue[0] = textBox83.Text;
            _learningValue[1] = textBox96.Text;
            _learningValue[2] = textBox95.Text;
            _learningValue[3] = textBox94.Text;
            _learningValue[4] = textBox93.Text;
            _learningValue[5] = textBox92.Text;
            _learningValue[6] = textBox91.Text;
            _learningValue[7] = textBox90.Text;
            _learningValue[8] = textBox6.Text;
        }
        private void SetLearningDataDocumentValue()
        {
            _learningDataDocumentValue = new string[8];
            if (maskedTextBox1.Text == DateEmptyLong)
                _learningDataDocumentValue[0] = DateLong;
            else
            {
                string str = GetMySqlDateTime(GetYear(maskedTextBox1.Text),
                                                 GetMounth(maskedTextBox1.Text),
                                                           GetDay(maskedTextBox1.Text),
                                                                  GetHours(maskedTextBox1.Text),
                                                                           GetMinutes(maskedTextBox1.Text),
                                                                                      "00");
                _learningDataDocumentValue[0] = str;
            }
            _learningDataDocumentValue[1] = textBox32.Text;
            _learningDataDocumentValue[2] = textBox31.Text;
            _learningDataDocumentValue[3] = textBox30.Text;
            _learningDataDocumentValue[4] = textBox29.Text;
            _learningDataDocumentValue[5] = textBox28.Text;
            _learningDataDocumentValue[6] = textBox27.Text;
            _learningDataDocumentValue[7] = textBox26.Text;
        }
        private void SetParentsValue()
        {
            _parentsValue = new string[4];
            _parentsValue[0] = textBox4.Text;
            _parentsValue[1] = textBox3.Text;
            _parentsValue[2] = textBox2.Text;
            _parentsValue[3] = textBox1.Text;
        }
        private void SetPaymentsValue()
        {
            _paymentsValue = new string[3];
            for(var i = 0; i<3; i++)
            {
                _paymentsValue[i] = string.Empty;
            }
        }
        private void SetPreviousEducationValue()
        {
            _previousEducationValue = new string[9];
            _previousEducationValue[0] = textBox64.Text;
            _previousEducationValue[1] = textBox63.Text;
            _previousEducationValue[2] = textBox62.Text;
            _previousEducationValue[3] = textBox61.Text;
            _previousEducationValue[4] = textBox60.Text;
            if (maskedTextBox2.Text == DateEmptyShort)
                _previousEducationValue[5] = DateShort;
            else
            {
                string str = GetMySqlDate(GetYear(maskedTextBox2.Text),
                                      GetMounth(maskedTextBox2.Text),
                                      GetDay(maskedTextBox2.Text));
                _previousEducationValue[5] = str;
            }
            _previousEducationValue[6] = textBox37.Text;
            _previousEducationValue[7] = textBox36.Text;
            _previousEducationValue[8] = textBox34.Text;
        }
        private void SetPrivateDataValue()
        {
            _privateDataValue = new string[16];
            _privateDataValue[0] = textBox46.Text;
            _privateDataValue[1] = textBox59.Text;
            _privateDataValue[2] = textBox58.Text;
            _privateDataValue[3] = textBox57.Text;
            _privateDataValue[4] = textBox56.Text;
            _privateDataValue[5] = textBox55.Text;
            _privateDataValue[6] = textBox54.Text;
            _privateDataValue[7] = textBox53.Text;
            if (maskedTextBox3.Text == DateEmptyShort)
                _privateDataValue[8] = DateShort;
            else
            {
                string str = GetMySqlDate(GetYear(maskedTextBox3.Text),
                                      GetMounth(maskedTextBox3.Text),
                                      GetDay(maskedTextBox3.Text));
                _privateDataValue[8] = str;
            } 
            _privateDataValue[9] = textBox45.Text;
            _privateDataValue[10] = textBox50.Text;
            _privateDataValue[11] = textBox51.Text;
            _privateDataValue[12] = textBox49.Text;
            _privateDataValue[13] = textBox48.Text;
            _privateDataValue[14] = textBox47.Text;
            _privateDataValue[15] = textBox5.Text;
        }

        private bool IsAllMandatoryField()
        {
            if(textBox46.Text != string.Empty &&
               textBox53.Text != string.Empty &&
               textBox44.Text != string.Empty &&
               textBox43.Text != string.Empty &&
               textBox42.Text != string.Empty &&
               textBox41.Text != string.Empty &&
               textBox40.Text != string.Empty &&
               textBox39.Text != string.Empty &&
               textBox23.Text != string.Empty)
            {
                return true;
            }
            return false;
        }
        private void Button1Click(object sender, EventArgs e)
        {
            if (IsAllMandatoryField())
            {
                SetDocumentStudentValue();
                SetExamsValue();
                SetJobsValue();
                SetLearningDataDocumentValue();
                SetLearningDataValue();
                SetLearningValue();
                SetParentsValue();
                SetPaymentsValue();
                SetPreviousEducationValue();
                SetPrivateDataValue();
                var f1 = Owner as FormMain;
                if (f1 != null)
                {
                    f1.GetParametres(_documentStudentValue, _examsValue, _jobsValue, _learningDataValue, _learningValue,
                                     _learningDataDocumentValue, _parentsValue, _paymentsValue, _previousEducationValue,
                                     _privateDataValue, "Insert");
                }
                Close();
            }
            else
            {
                MessageBox.Show(@"Не все обязательные поля заполнены!", @"Ошибка", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
        }

        private void TextBox73KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0') && (e.KeyChar <= '9'))
                return;
            if (Char.IsControl(e.KeyChar))
                return;
            e.Handled = true;
        }
        private void TextBox72KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0') && (e.KeyChar <= '9'))
                return;
            if (Char.IsControl(e.KeyChar))
                return;
            e.Handled = true;
        }
        private void TextBox81KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0') && (e.KeyChar <= '9'))
                return;
            if (Char.IsControl(e.KeyChar))
                return;
            e.Handled = true;
        }
        private void TextBox22KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0') && (e.KeyChar <= '9'))
                return;
            if (Char.IsControl(e.KeyChar))
                return;
            e.Handled = true;
        }

        private string GetYear(string str)
        {
            string res = str[6].ToString() + str[7].ToString() + str[8].ToString() + str[9].ToString();
            return res;
        }
        private string GetMounth(string str)
        {
            string res = str[3].ToString() + str[4].ToString();
            return res;
        }
        private string GetDay(string str)
        {
            string res = str[0].ToString() + str[1].ToString();
            return res;
        }
        private string GetHours(string str)
        {
            string res = str[11].ToString() + str[12].ToString();
            return res;
        }
        private string GetMinutes(string str)
        {
            string res = str[14].ToString() + str[15].ToString();
            return res;
        }
        private string GetMySqlDateTime(string year, string mounth, string day, string hours, string minutes, string seconds)
        {
            string res = string.Empty;
            res += year;
            res += "-";
            res += mounth;
            res += "-";
            res += day;
            res += " ";
            res += hours;
            res += ":";
            res += minutes;
            res += ":";
            res += seconds;
            return res;
        }
        private string GetMySqlDate(string year, string mounth, string day)
        {
            string res = string.Empty;
            res += year;
            res += "-";
            res += mounth;
            res += "-";
            res += day;
            return res;
        }
    }
}
